package com.ssm.controller;

import com.ssm.pojo.Build;
import com.ssm.service.BuildService;
import com.ssm.vo.BuildVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("build")
public class BuildController {
    @Autowired
    private BuildService buildService;

    //查询所有的楼栋号，并以json的格式返回
    @RequestMapping(value = "/findAllBuildNumber",method = RequestMethod.GET)
    @ResponseBody
    public List<Build> findAllBuildNumber(){
        List<Build> list = buildService.findAllBuildNumber();
        return list;
    }

    @RequestMapping("/selectBuild")
    @ResponseBody
    public BuildVo userTypeList(int page,int limit){
        BuildVo BuildVo = new BuildVo();
        BuildVo.setCode(0);
        BuildVo.setMsg("");
        List<Build> buildTypes = buildService.findAllBuildNumber();
        //截取当前页面需要显示的数据

        //计算出当前页数据开始的索引
        int start = (page - 1) * limit;//开始的索引
        int end = page*limit;//结束的索引
        //获取到可以subList的最大索引
        int count = buildTypes.size();
        List<Build> data;
        if(end>count){
            if(count==end-limit){
                data = buildTypes.subList(start-limit,count);
            }else {
                data = buildTypes.subList(start,count);
            }
        }else{
            data = buildTypes.subList(start,end);
        }
        BuildVo.setCount(count);
        BuildVo.setData(data);
        return BuildVo;
    }

    //添加楼栋号
    @RequestMapping(value = "/addBuild",method = RequestMethod.POST)
    @ResponseBody
    public String addBuild(Build build){
        int i = buildService.addBuild(build);
        if(i>=1){
            return "ADD SUCCESS";
        }else {
            return "ADD FAIL";
        }
    }


    //修改楼层信息
    @RequestMapping(value = "/updateBuildNumber",method = RequestMethod.POST)
    @ResponseBody
    public String editUserType(Build build){
        System.out.println("修改");
        System.out.println(build);
        int i = buildService.updateBuildNumber(build);
        System.out.println(i);
        if(i>=1){
            return "EDIT SUCCESS";
        }else {
            return "EDIT FAIL";
        }

    }

    //删除楼层信息
    @RequestMapping("/deleteBuildById")
    @ResponseBody
    public String deleteBuildById(int buildId){
        System.out.println("删除了");
        int i = buildService.deleteBuildById(buildId);
        System.out.println(i);
        if(i>=1){
            return "DELETE SUCCESS";
        }else {
            return "DELETE FAIL";
        }

    }

    //批量删除所选的楼栋号
    @RequestMapping(value = "/deleteBuilds",method = RequestMethod.POST)
    @ResponseBody
    public String deleteBuilds(int[] ids){

//        String res = "(";
//        for (int i = 0; i < ids.length; i++) {
//            if(i==ids.length-1){
//                res += ids[i]+")";
//            }else {
//                res += ids[i]+",";
//            }
//        }
//        System.out.println(res);
       if (ids!=null){
           int i = buildService.deletebuilds(ids);
           System.out.println(i);
           if(i>=1){
               return "BATCHDELETE SUCCESS";
           }else {
               return "DELETE FAIl";
           }
       }
       return "Please Select";

    }

    //查询操作
    @RequestMapping("/select")
    @ResponseBody
    public BuildVo getUserTypes(Integer buildId, Integer buildNumber, int page, int limit){
        BuildVo buildVo = new BuildVo();
        List<Build> builds = null;
        try {
            if(buildId==null){
                //如果查询的buildId为空的话，通过查询楼栋号来查询
                builds = buildService.selectByBuildNumber(buildNumber);
            }else if (buildNumber==null){
                builds = buildService.selectByBuildId(buildId);
            }else if (buildId!=0&&buildNumber!=0){
                builds = buildService.selectByBuildIdAndBuildNumber(buildId,buildNumber);
            }else {
                builds = buildService.queryAll();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println("进入到finally");
            buildVo.setCode(0);
            buildVo.setMsg("");
            //截取当前页面需要显示的数据
            System.out.println("进入到finally里面");
            //计算出当前页数据开始的索引
            int start = (page - 1) * limit;//开始的索引
            int end = page*limit;//结束的索引
            //获取到可以subList的最大索引
            int count = builds.size();
            List<Build> data;
            if(end>count){
                if(count==end-limit){
                    data = builds.subList(start-limit,count);
                }else {
                    data = builds.subList(start,count);
                }
            }else{
                data = builds.subList(start,end);
            }
            buildVo.setCount(count);
            buildVo.setData(data);
        }
        return buildVo;
    }

}
